package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
* @author 11258
* @description 针对表【employee(员工信息)】的数据库操作Service实现
* @createDate 2024-03-21 16:27:12
*/
@Slf4j
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee>
    implements EmployeeService{

    @Resource
    private EmployeeMapper employeeMapper;



    /**
     * 员工登录
     * @param request
     * @param employee
     * @return
     */
    @Override
    public R<Employee> login(HttpServletRequest request, Employee employee) {
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("username",employee.getUsername());
        Employee emp = employeeMapper.selectOne(wrapper);
        //判断是否有此用户
        if(emp == null){
            return R.error("没有找到此用户！！！");
        }
        //判断密码是否正确
        if(!password.equals(emp.getPassword())){
            return R.error("密码错误！！！");
        }
        //判断员工的状态？1 代表正常，0 表示禁用
        if(emp.getStatus()==0){
            return R.error("此用户已被禁用！！！");
        }
        request.getSession().setAttribute("employee",emp.getId());
        log.info("用户登录成功！");
        return R.success(emp);
    }

    /**
     * 员工登出
     * @param request
     * @return
     */
    @Override
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        log.info("用户退出成功！");
        return R.success("退出成功！");
    }

    @Override
    public R<String> add(HttpServletRequest request,Employee employee) {
        log.info("新增员工,员工信息：{}",employee.toString());
        //设置初始密码为123456
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);

//        mybatisplus自动填充字段
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        //获取当前登录用户的id
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);

        employeeMapper.insert(employee);
        log.info("新增员工Successfully");
        return R.success("新增员工成功！");
    }

    @Override
    public R<String> set(HttpServletRequest request,Employee employee) {
       log.info(employee.toString());


//       employee.setUpdateTime(LocalDateTime.now());
//       Long empId = (Long) request.getSession().getAttribute("employee");
//       employee.setUpdateUser(empId);
       employeeMapper.updateById(employee);
       return R.success("员工信息修改成功!");
    }
}




